import com.adobe.cairngorm.control.CairngormEventDispatcher;

import flash.events.Event;

import mx.controls.Alert;
import mx.events.CloseEvent;

import net.optix.ip.events.RequestBidEvent;
import net.optix.ip.model.ModelLocator;
import net.optix.ip.vo.RequestBidVO;

import views.ViewInstance;

private function initApp():void
{
	//trace(usdFormatter.format(model.currentInvention.maxBudget.toString())+"   usdFormatter.format(model.currentInvention.maxBudget.toString())");
}

private function myformat(event:Event):void
{
	try
	{
		event.target.text	=	usdFormatter.format(event.target.text);
		
	}
	catch(error:Error)
	{
	}
	
}
[Bindable]
private var model:ModelLocator	=	ModelLocator.getInstance();

private function showMoney(event:Event):void
{
	event.target.text	=	purseMoney(getMoney(event.target.text));
}

private function getMoney(money:String):String
{
	var str:String		=	"";
	for(var i:int=0; i<money.length; i++)
	{
		if(money.charAt(i) != "," && money.charAt(i) != " ")
		{
			str	=	str+money.charAt(i);
		}
	}
	return str;
}

private function purseMoney(money:String):String
{
	var arr:Array	=	money.split(".");
	if(arr.length == 1)
	{
		return purseMoneyBefore(money);
	}
	else if(arr.length == 2)
	{
		var str1:String;
		str1	=	purseMoneyBefore(arr[0])+" . "+purseMoneyAfter(arr[1]);
		return str1;
	}
	return "";
}

private function purseMoneyBefore(money:String):String
{
	var str:String	=	money.charAt(0);
	var length:int	=	money.length;
		
	for(var i:int=1; i<money.length; i++)
	{
		if(((length-i)/3).toString().length == 1)
		{
			str	=	str+" , "+money.charAt(i);
		}
		else
		{
				
			str	=	str+money.charAt(i);
		}
	}
	return str;
}

private function purseMoneyAfter(money:String):String
{
	var str:String	=	money.charAt(0);
	var length:int	=	money.length;
		
	for(var i:int=1; i<money.length; i++)
	{
		if((i/3).toString().length == 1)
		{
			str	=	str+" , "+money.charAt(i);
		}
		else
		{
				
			str	=	str+money.charAt(i);
		}
	}
	return str;
}

private function requestBid(event:Event):void
{
	if(model.client.bFileOut == false)
	{
		//ViewInstance.showMessage('','Fill out your company information first !');
		clickHandler();
	}
	else
	{
		var result:Boolean	=	true;
		var message:String	=	"";
		var maxBudgetI:int	=	int(getMoney(maxBudget.text));
		var targetDateI:String	=	targetDate.text;
		
		message	=	"";
		
		if(maxBudgetI == 0)
		{
			message	=	"Maxmum budget is required . ";
			result	=	false;
		}
		
		if(targetDateI == "")
		{
			message	+=	"Filling due date is required . ";
			result	=	false;
		}
		
		if(result == true)
		{
			model.bSubmit	=	false;
			resultRequest.text	=	"";
			
			
			var vo:RequestBidVO		=	new RequestBidVO();
			
			vo.id				=	model.currentInvention.inventionID;
			vo.category			=	0;
			vo.bBid				=	1;
			vo.maxBudget		=	maxBudgetI;
			vo.fillingDate		=	targetDate.text;
			
			var requestBidEvent:RequestBidEvent	=	new RequestBidEvent(vo);
			CairngormEventDispatcher.getInstance().dispatchEvent(requestBidEvent);
		}
		else
		{
			resultRequest.text	=	message;
		}
	}
	
}

private function clickHandler():void
{
	Alert.yesLabel = "Yes";
 	Alert.show("Fill out your company information first !", "", Alert.OK, this, alertClickHandler);
}

private function alertClickHandler(event:CloseEvent):void 
{
	model.actionView	=	0;
	model.actionView	=	ViewInstance.EditBilling;
}